//
//  XZMyQuestionCell.m
//  Teecloud
//
//  Created by PC on 2018/10/29.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMyQuestionCell.h"
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
@interface XZMyQuestionCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>
//问跟回答的标签
@property (weak, nonatomic) IBOutlet UILabel *aksLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;
//问的时间
@property (weak, nonatomic) IBOutlet UILabel *askTimeLabel;
//问的内容
@property (weak, nonatomic) IBOutlet UILabel *askContentLabel;
/***********************AttachmentView**********************/
@property (weak, nonatomic) IBOutlet UIView *AttachmentView;
//Attachment图片
@property (weak, nonatomic) IBOutlet UIImageView *AttachmentImg;
//Attachment ResName
@property (weak, nonatomic) IBOutlet UILabel *AttachmentResNameLabel;
//Attachment PositionStr
@property (weak, nonatomic) IBOutlet UIButton *AttachementPositionBtn;

//回答问题的人的头像
@property (weak, nonatomic) IBOutlet UIImageView *answerImg;
//回答问题的人的姓名
@property (weak, nonatomic) IBOutlet UILabel *answerNameLabel;
//回答内容
@property (weak, nonatomic) IBOutlet UILabel *answerContentLabel;


//问题view的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *questionHeight;
//回答view的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *answerHeight;

//问的view
@property (weak, nonatomic) IBOutlet UIView *askView;
//回复的view
@property (weak, nonatomic) IBOutlet UIView *answerView;

/***********Tableview/CollectionView***************/
@property (weak, nonatomic) IBOutlet UITableView *askTableView;
@property (weak, nonatomic) IBOutlet UITableView *answerTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *askCollectView;
@property (weak, nonatomic) IBOutlet UICollectionView *answerCollectView;

/*******************NSLayoutConstraint**************************/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *askTableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *askCollectViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *answerTableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *answerCollectionViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *AttachementViewHeight;

@end

@implementation XZMyQuestionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.aksLabel.layer.cornerRadius = 3.0f;
    self.aksLabel.clipsToBounds = YES;
    
    self.answerLabel.layer.cornerRadius = 3.0f;
    self.answerLabel.clipsToBounds = YES;
    
    
    //TableView的属性设置
    _askTableView.delegate =self;
    _askTableView.dataSource = self;
    _askTableView.scrollEnabled = false;
    [_askTableView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    
    _answerTableView.delegate = self;
    _answerTableView.dataSource = self;
    _answerTableView.scrollEnabled = false;
    [_answerTableView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    
    //Collectionview的属性设置
    _askCollectView.scrollEnabled = false;
    _askCollectView.backgroundColor = kWhiteColor;
    _askCollectView.dataSource = self;
    _askCollectView.delegate = self;
    [_askCollectView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    
    _answerCollectView.scrollEnabled = false;
    _answerCollectView.backgroundColor = kEAEAEAColor;
    _answerCollectView.dataSource = self;
    _answerCollectView.delegate = self;
    [_answerCollectView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    
}

- (void)setModel:(XZQuestionData *)model{
    _model = model;
    /******* 问题*********/
    self.askTimeLabel.text = model.Question.Time;
    [self.AttachmentImg yy_setImageWithURL:[NSURL URLWithString:model.Question.Attachment.HeadUrl] placeholder:DefealtImg];
    self.AttachmentResNameLabel.text =model.Question.Attachment.ResName;
    //大于2的时候不可以点击
    if ([model.Question.Attachment.ResType integerValue] > 2) {
        self.AttachmentResNameLabel.textColor = kBlackColor;
        self.AttachmentResNameLabel.userInteractionEnabled = false;
    }else{
        self.AttachmentResNameLabel.userInteractionEnabled = true;
        //添加点击事件
        UITapGestureRecognizer *lookTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookTapAction)];
        [self.AttachmentResNameLabel addGestureRecognizer:lookTap];
    }
    //视频播放的位置
    if ([model.Question.Attachment.ResType isEqualToNumber:@(1)] && model.Question.Attachment.PositionStr !=nil) {
        self.AttachementPositionBtn.hidden = false;
        [self.AttachementPositionBtn setTitle:[NSString stringWithFormat:@"【%@】",model.Question.Attachment.PositionStr] forState:UIControlStateNormal];
    }else{
        self.AttachementPositionBtn.hidden = true;
    }

    //评论
    self.askContentLabel.text = model.Question.Content;
    CGFloat askContentHight = [self.askContentLabel getLableSizeWithMaxWidth:(DEVICE_WIDTH-45)].height;
    //语音
    if (model.Question.VoiceList.count == 0) {
        self.askTableViewHeight.constant = 0;
        self.askTableView.hidden = true;
    }else{
        self.askTableViewHeight.constant = model.Question.VoiceList.count *30;
        self.askTableView.hidden = false;
        [self.askTableView reloadData];
    }
    //图片
    if (model.Question.ImgFileList.count == 0) {
        self.askCollectViewHeight.constant = 0;
        self.askCollectView.hidden = true;
    }else{
        self.askCollectViewHeight.constant = 60;
        self.askCollectView.hidden = false;
        [self.askCollectView reloadData];
    }
    
    if (model.Question.Attachment != nil) {
        self.AttachmentView.hidden = false;
        self.AttachmentImg.hidden = false;
        self.AttachmentResNameLabel.hidden = false;
        self.AttachementPositionBtn.hidden = false;
        self.AttachementViewHeight.constant = 30;
    }else{
        self.AttachmentView.hidden = true;
        self.AttachmentImg.hidden = true;
        self.AttachmentResNameLabel.hidden = true;
        self.AttachementPositionBtn.hidden = true;
        self.AttachementViewHeight.constant = 0;
    }
    
    self.questionHeight.constant = 36+askContentHight+self.askTableViewHeight.constant+self.askCollectViewHeight.constant+self.AttachementViewHeight.constant+6;
    
    /**************   回复   ******* *********/
    self.answerNameLabel.hidden = false;
    self.answerImg.hidden = false;
    if (model.Answer != nil) {
        
        //评论
        self.answerContentLabel.text = model.Answer.Content;
        CGFloat answerContentHight = [self.answerContentLabel getLableSizeWithMaxWidth:(DEVICE_WIDTH-45)].height;
        //语音
        if (model.Answer.VoiceList.count == 0) {
            self.answerTableViewHeight.constant = 0;
            self.answerTableView.hidden = true;
        }else{
            self.answerTableViewHeight.constant = model.Question.VoiceList.count *30;
            self.answerTableView.hidden = false;
            [self.answerTableView reloadData];
        }
        //图片
        if (model.Answer.ImgFileList.count == 0) {
            self.answerCollectionViewHeight.constant = 0;
            self.answerCollectView.hidden = true;
        }else{
            self.answerCollectionViewHeight.constant = 60;
            self.answerCollectView.hidden = false;
            [self.answerCollectView reloadData];
        }
        self.answerHeight.constant = 39+answerContentHight+self.answerTableViewHeight.constant+self.answerCollectionViewHeight.constant+6;
    }else{
        //空的话就显示暂无
        self.answerHeight.constant = 40;
        self.answerTableViewHeight.constant = 0;
        self.answerCollectionViewHeight.constant = 0;
        self.answerNameLabel.hidden = YES;
        self.answerImg.hidden = YES;
        [self.answerContentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        //没有数据的时候添加暂无标签
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = @"暂无";
        [self.answerView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.answerLabel.mas_right).offset(8);
            make.centerY.mas_equalTo(self.answerLabel);
        }];
    }
    //cell的总高度
    model.cellHeight = self.questionHeight.constant+self.answerHeight.constant;
}
/**
 *
 *ResType=1：跳转”主页 - 我的 - 我的资源 - 播放 - 问题“页面
 
 ResType=2：跳转”主页 - 我的 - 我的资源 - 文档“页面
 *
 */
- (void)lookTapAction{
    if (self.lookBlock) {
        self.lookBlock(self.model);
    }
}
//跳转到指定的播放位置
- (IBAction)AttachementPositionButtonAction:(UIButton *)sender {
    if (self.playBlock) {
        self.playBlock(self.model);
    }
}



#pragma mark - CollectViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.askCollectView]) {
        return self.model.Question.ImgFileList.count;
    }
    return self.model.Answer.ImgFileList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
    GMAttachListModel *model= [[GMAttachListModel alloc]init];
    if ([collectionView isEqual:self.askCollectView]) {
        model= self.model.Question.ImgFileList[indexPath.row];
    }else{
        model= self.model.Answer.ImgFileList[indexPath.row];
    }
    [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
    cell.delectBtn.hidden=YES;
    return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    /**放大图片*/
    GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
    NSMutableArray *items = @[].mutableCopy;
    
    if ([collectionView isEqual:self.askCollectView]) {
        for (int i = 0; i < self.model.Question.ImgFileList.count; i++){
            GMAttachListModel *model= self.model.Question.ImgFileList[i];
            UIImageView *imgV = cell.photoImg;
            KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
            [items addObject:item];
        }
    }else{
        for (int i = 0; i < self.model.Answer.ImgFileList.count; i++){
            GMAttachListModel *model= self.model.Answer.ImgFileList[i];
            UIImageView *imgV = cell.photoImg;
            KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
            [items addObject:item];
        }
    }
    
    KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
    browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
    browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
    browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
    browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
    browser.bounces = NO;
    [browser showFromViewController:self.parentController];
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.askTableView]) {
        return  self.model.Question.VoiceList.count;
    }
    return self.model.Answer.VoiceList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *voiceStr = nil;//Question Answer 的语音标识
    GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
    GMVoiceListModel *model = [[GMVoiceListModel alloc]init];
    if ([tableView isEqual:self.askTableView]) {
         voiceStr = @"Question";
        model = self.model.Question.VoiceList[indexPath.row];
    }else{
        voiceStr = @"Answer";
        model = self.model.Answer.VoiceList[indexPath.row];
    }
    kWeakObject(self)
    //语音读与未读
    cell.readBlock = ^{
        weakself.hasReadBlock(indexPath.row, model.Id,voiceStr);
    };
    cell.model = model;
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}


@end
